package com.yannan.lock.web.controller;//package com.yannan.lock.web.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.yannan.lock.core.constant.CoreConstant;
import com.yannan.lock.core.controller.BaseController;
import com.yannan.lock.core.vo.ResultResponse;
import com.yannan.lock.web.pojo.SelectGateWayByPlot;
import com.yannan.lock.web.pojo.SelectgetGateway;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yannan.lock.web.service.InstallationService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ArerInfoController
 * 小区信息处理
 *
 * @author wu
 * @create 2016-10-10 18:02
 */
@Controller
public class ArerInfoController extends BaseController{

    /** Field installationService ... */
    @Resource
    private InstallationService installationService;





    /**
     *查询单元信息
     */
    @RequestMapping("/getUnitInfo")
    @ResponseBody
    public   ResultResponse getUnitInfo(HttpServletRequest request) {
        ResultResponse res = new ResultResponse();

        String buildingId=request.getParameter("buildingId");
        if(StringUtils.isEmpty(buildingId)){
            res.setRetCode(CoreConstant.RET_CODE_FAILED);
            res.setMessage("请选择小区(buildingId)");
            return  res;
        }

        try {
            List<SelectGateWayByPlot> list=installationService.selectUnitInfoByBuildingId(buildingId);
            res.setMessage("getFloor_ok");
            res.setRetCode(CoreConstant.RET_CODE_SUCCESS);
            res.setData(list);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            res.setRetCode(CoreConstant.RET_CODE_FAILED);
            res.setMessage("出错了！"+e.getMessage());
            return  res;
        }
        return res;
    }
    /**
     *查询楼层的网关信息
     */
    @RequestMapping("/getGatewayOrfloorInfo")
    @ResponseBody
    public   ResultResponse getGatewayOrfloorInfo(HttpServletRequest request){
        ResultResponse mr=new ResultResponse();
        try {
            String  plotId=request.getParameter("plotId");
            if (StringUtils.isEmpty(plotId)){
                mr.setRetCode(CoreConstant.RET_CODE_FAILED);
                mr.setMessage("请选择小区");
                return mr;
            }
            String  buildingId=request.getParameter("buildingId");
            if (StringUtils.isEmpty(buildingId)){
                mr.setMessage("请选择楼号");
                mr.setRetCode(CoreConstant.RET_CODE_FAILED);
                return mr;
            }
            String  unitId=request.getParameter("unitId");
            if (StringUtils.isEmpty(unitId)){
                mr.setMessage("请选择单元");
                mr.setRetCode(CoreConstant.RET_CODE_FAILED);
                return mr;
            }
            String  floorId=request.getParameter("floorId");
            if (StringUtils.isEmpty(floorId)){
                mr.setMessage("请选择楼层");
                mr.setRetCode(CoreConstant.RET_CODE_FAILED);
                return mr;
            }
            String  typeId=request.getParameter("typeId");
            if (StringUtils.isEmpty(typeId)){
                mr.setMessage("请选择查看类型");
                mr.setRetCode(CoreConstant.RET_CODE_FAILED);
                return mr;
            }
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("neighbourhoodsId",plotId);
            map.put("buildingId",buildingId);
            map.put("unitId",unitId);
            map.put("floorId",floorId);
            if(typeId.equals("0")){//查询网关信息
                List<SelectgetGateway> list=installationService.selectgetGateway(map);
                if(list.size()==0){
                    mr.setMessage("没有对应网关");
                    mr.setRetCode(CoreConstant.RET_CODE_FAILED);
                    return mr;
                }
                mr.setRetCode(CoreConstant.RET_CODE_FAILED);
                mr.setRetCode("getGateway_ok");
                mr.setData(list);
                return mr;
            }else if (typeId.equals("1")){
                List<SelectgetGateway> list=installationService.selectgetFloor(map);
                if(list.size()==0){
                    mr.setMessage("没有对应房间");
                    mr.setRetCode(CoreConstant.RET_CODE_FAILED);
                    return mr;
                }
                mr.setMessage("getFloor_ok");
                mr.setRetCode(CoreConstant.RET_CODE_SUCCESS);
                mr.setData(list);
                return mr;
            }


            return mr;

        }catch (Exception e){
            e.printStackTrace();
            mr.setRetCode(CoreConstant.RET_CODE_FAILED);
            mr.setMessage("查询错误");
            return mr;
        }
    }

}


//~ Formatted by Jindent --- http://www.jindent.com
